<template>
  <div>
    <el-popover placement="bottom" width="300" trigger="click">
      <el-button icon="el-icon-plus" slot="reference" class="gray" size="mini" round>选择标签</el-button>
      <el-checkbox-group v-model="form.tagId" class="margin-bottom">
        <div class="flex align-center justify-between" v-for="(item, index) in tagList" :key="item.id">
          <el-checkbox class="margin-bottom-xs" :label="item.id">{{ item.tagName }}</el-checkbox>
          <i class="el-icon-delete text-l margin-lr-xs" v-if="item.id !== 0" @click="onTagRemove(item.id, index)"></i>
        </div>
      </el-checkbox-group>
      <el-button v-if="!visible" size="mini" @click="visible = true">新建</el-button>
      <div class="flex" v-else>
        <el-input style="width: 160px" v-focus v-model.trim="value" size="small"></el-input>
        <el-link
          class="margin-lr-sm"
          type="primary"
          :underline="false"
          @click="onTagCreate"
          :disabled="loading || !value"
          >创建</el-link
        >
        <el-link
          :underline="false"
          @click="
            visible = false;
            value = '';
          "
          >取消</el-link
        >
      </div>
    </el-popover>
    <span style="margin-left: 8px" class="gray">设置标签后，可以通过标签分类、搜索</span>
    <br />
    <el-tag
      size="small"
      v-for="(item, index) in form.tagId"
      :key="item"
      color="#fff"
      closable
      @close="onTagClose(index)"
      >{{ tagList.find(tag => tag.id === item) ? tagList.find(tag => tag.id === item).tagName : item }}</el-tag
    >
  </div>
</template>

<script>
import { getMaterialTags, createMaterialTag, deleteMaterialTag } from "@/api/material/category/tag";
export default {
  name: "materialTag",
  props: {
    form: {
      type: Object,
      required: true
    },
    module: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      tagList: [],
      visible: false,
      loading: false,
      value: ""
    };
  },
  created() {
    getMaterialTags(this.module.value).then(res => {
      if (res.success) {
        this.tagList = res.voList;
      }
    });
  },
  methods: {
    // 创建标签
    onTagCreate() {
      this.loading = true;
      createMaterialTag({
        materialType: this.module.value,
        tagName: this.value
      })
        .then(res => {
          if (res.success) {
            this.$message({
              message: "创建成功！",
              type: "success"
            });
            this.tagList.push(res.vo);
            this.visible = false;
            this.value = "";
          }
        })
        .finally(() => (this.loading = false));
    },
    // 删除标签
    onTagClose(index) {
      this.$confirm("确定要删除该项？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.form.tagId.splice(index, 1);
      });
    },
    // 移除标签
    onTagRemove(id, index) {
      this.$confirm("确定要删除该项？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteMaterialTag({ id }).then(res => {
          if (res.success) {
            this.$message({
              message: "删除成功！",
              type: "success"
            });
            if (this.form.tagId.includes(id)) this.form.tagId = this.form.tagId.filter(item => item !== id);
            this.tagList.splice(index, 1);
          }
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-tag {
  padding: 0 10px;
  border-radius: 12px;
  margin: 5px 5px 5px 0;
}
</style>
